package com.orm.analysis.action;

import com.opensymphony.xwork2.ActionContext;
import com.orm.analysis.model.*;
import com.orm.analysis.service.GenericService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class AddressAction extends FrontendAction {

    private GenericService<Address> service;
    private GenericService<InternationalAddress> interAddressservice;
    private Address address;
    private InternationalAddress interAddress;
    private AddressType[] addressTypeList;
    private List<Address> addresses;


    public AddressAction(GenericService<Address> service,GenericService<InternationalAddress> interAddressservice) {
        this.service = service;
        this.interAddressservice = interAddressservice;
    }

    public String getAllAddresses() {
        addresses = service.list(page, size);
        return SUCCESS;
    }

    public String list() {
        addresses = service.list(page, size);
        return SUCCESS;

    }

    public String setUpForInsertOrUpdate() {
        prep();
        if (address != null && address.getAddressId() != null  ) {

            address = service.getById(address.getAddressId());
        }
        return "success";
    }


    public String insertOrUpdate() {
        prep();
        if (!validationSuccessful()) {
            return "input";
        } else {
            if (address.getAddressId() == null) {
                service.create(address);
            } else {
                service.update(address);
            }

        }
        return "success";
    }

    public String interAddressSetUpForInsertOrUpdate() {
        prep();
        if (address != null && address.getAddressId() != null  ) {

            address = interAddressservice.getById(address.getAddressId());
        }
        return "success";
    }


    public String interAddressInsertOrUpdate() {
        prep();
        if (!validationSuccessful()) {
            return "input";
        } else {
            if (address.getAddressId() == null) {
                interAddressservice.create((InternationalAddress)address);
            } else {
                interAddressservice.update((InternationalAddress)address);
            }

        }
        return "success";
    }

    private void prep() {
        addressTypeList = new AddressType[4];
        addressTypeList[0]=AddressType.BILLING;
        addressTypeList[1]=AddressType.HOME;
        addressTypeList[2]=AddressType.OFFICE;
        addressTypeList[3]=AddressType.SHIPPING;

        Map session = ActionContext.getContext().getSession();
   		session.put("addressTypeList", addressTypeList);
   	}


    public String delete() {
        service.delete(address.getAddressId());
        return "success";
    }

    public String update() {
        service.update(address);
        return SUCCESS;
    }

    public String create() {
        service.create(address);
        return SUCCESS;
    }


    private boolean validationSuccessful() {
        if (address.getStreet() == null || address.getStreet().trim().length() < 1) {
            addActionMessage("Street is required");
        }
        if (address.getZipcode() == null || address.getZipcode().trim().length() < 1) {
            addActionMessage("Zipcode is required");
        }

        if (this.hasActionMessages()) {
            return false;
        } else {
            return true;
        }


    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public InternationalAddress getInterAddress() {
        return interAddress;
    }

    public void setInterAddress(InternationalAddress interAddress) {
        this.interAddress = interAddress;
    }
}
